﻿using Helldivers_Alpha.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Helldivers_Alpha.DataFrom
{
    public partial class SupplyStratagemsFrom : Form
    {
        List<DefaultMod> maindata;
        List<PictureBox> pictureBoxesList =new List<PictureBox>();
        List<Label> labelsList =new List<Label>();
        int CwId;
        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="mods">载入数据</param>
        /// <param name="Cw">槽位id</param>
        public SupplyStratagemsFrom(List<DefaultMod> mods,int Cw)
        {
            CwId = Cw;
            maindata = mods;
            InitializeComponent();
            this.Text = "战略配备槽位"+ Cw;
        }

        private void SupplyStratagems_Load(object sender, EventArgs e)
        {
            LoadData(kind.补给类战略配备);
        }

        /// <summary>
        /// 载入数据
        /// </summary>
        private void LoadData(kind kind)
        {
            //初始位置
            int startLocationX = 30;
            int startLocationy = 12;
            int startLocationNum = 0;
            //清理之前的控件
            foreach (var item in pictureBoxesList)
            {
                this.Controls.Remove(item);
            }
            foreach (var item in labelsList)
            {
                this.Controls.Remove(item);
            }
            pictureBoxesList.Clear();
            labelsList.Clear();
            //生成
            var finddata = maindata.Where(K => K.Kind == kind).ToList();
            foreach (var item in finddata)
            {
                if (startLocationNum >= 5)
                {
                    startLocationNum = 0;
                    startLocationy += 100;
                    startLocationX = 30;
                }
                PictureBox pictureBox = new PictureBox();
                pictureBox.Name = "PictureBox_" + item.Id;
                pictureBox.Location = new Point(startLocationX, startLocationy);
                pictureBox.Width = 48;
                pictureBox.Height = 48;
                pictureBox.Click += new EventHandler(btn_Click);
                pictureBox.Image = item.Img;
                this.Controls.Add(pictureBox);
                pictureBoxesList.Add(pictureBox);

                Label label = new Label();
                label.Text = item.Name;
                label.Location = new Point(startLocationX, startLocationy + 48);
                label.Width = 100;
                this.Controls.Add(label);
                labelsList.Add(label);
                //下一个空间 x增加55
                startLocationX += 100;
                startLocationNum++;
            }
        }

        /// <summary>
        /// 基础引用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox)sender;
            string checkname= pictureBox.Name;
            var checknamelist = checkname.Split('_');
            int checkid = Convert.ToInt32(checknamelist[1]);
            switch (CwId)
            {
                case 1:
                    Helldivers_Alpha.defaultModNO1 = maindata.FirstOrDefault(K => K.Id == checkid);
                    Helldivers_Alpha.DH01();
                    break;
                case 2:
                    Helldivers_Alpha.defaultModNO2 = maindata.FirstOrDefault(K => K.Id == checkid);
                    Helldivers_Alpha.DH02();
                    break;
                case 3:
                    Helldivers_Alpha.defaultModNO3 = maindata.FirstOrDefault(K => K.Id == checkid);
                    Helldivers_Alpha.DH03();
                    break;
                case 4:
                    Helldivers_Alpha.defaultModNO4 = maindata.FirstOrDefault(K => K.Id == checkid);
                    Helldivers_Alpha.DH04();
                    break;
                default:
                    break;
            }
           
            this.Close();
        }

        /// <summary>
        /// 补给类战略配备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            LoadData(kind.补给类战略配备);
        }

        /// <summary>
        /// 防御类战略配备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            LoadData(kind.防御类战略配备);
        }

        /// <summary>
        /// 空袭类战略配备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            LoadData(kind.空袭类战略配备);
        }
    }
}
